package lotteryGui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import autoRegister.Auto_Register;
import lotteryModel.Tickets;
import lotteryModel.Users;
import lotteryModel.MyThread;
import lotteryGui.Interphase;

public class Manager {
    public Manager() {
        initmanager();
    }
    public void initmanager() {
        JFrame menu = new JFrame();
        menu.setLayout(null); // 清除布局函数
        menu.setSize(300,400);
        menu.setResizable(false); // 设置窗体大小不可变
        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menu.setLayout(new BorderLayout()); // 新建BorderLayout布局

        JPanel panel1 = new JPanel(new FlowLayout());
        JLabel jl1 = new JLabel("欢迎使用管理员界面");
        jl1.setFont(new Font("微软雅黑", Font.BOLD, 30)); // 设置字体、样式、大小
        panel1.add(jl1);
        panel1.setBorder(new EmptyBorder(10, 10, 0, 10));
        menu.add(panel1, BorderLayout.NORTH);

        JPanel panel2 = new JPanel(new GridLayout(5, 1));




        JButton jb1 = new JButton("开奖");
        jb1.addActionListener(new java.awt.event.ActionListener() {
            @SuppressWarnings("deprecation")
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JFrame menu1 = new JFrame("开奖");
                menu1.setLayout(new BorderLayout());
                JPanel jp1 = new JPanel();
                JPanel jp2 = new JPanel();
                JTextField jf = new JTextField(15);
                jf.setFont(new Font("微软雅黑", Font.BOLD, 18));
                JButton jb1 = new JButton("开始");
                MyThread mt = new MyThread(jf);
                jb1.addActionListener((ActionEvent e)->{
                    jf.setText("");
                    mt.start();
                });
                JButton jb2 = new JButton("停止");
                jb2.addActionListener((ActionEvent e)->{
                    mt.stop();
                    modifyFile(jf);
                });
                jp1.add(jf);
                jp2.add(jb1);
                jp2.add(jb2);
                JPanel jpanel1 = new JPanel(new FlowLayout());
                JScrollPane scrollPane1 = new JScrollPane();
                scrollPane1.setBounds(80,25, 700, 500);
                jpanel1.add(scrollPane1);
                JTable table1 = new JTable();
                DefaultTableModel dtm = new DefaultTableModel(
                        new Object[][] {  },
                        new String[] { "ID","倍数","奖项"});
                GetMan(jf,dtm);
                table1.setModel(dtm);
                table1.setBounds(100, 100, 200, 200);
                scrollPane1.setPreferredSize(new Dimension(300, 200));
                scrollPane1.setAutoscrolls(true);
                scrollPane1.setViewportView(table1);
                menu1.add(jp1, BorderLayout.NORTH);
                menu1.add(jp2, BorderLayout.CENTER);
                menu1.add(jpanel1,BorderLayout.SOUTH);
                menu1.pack();
                location(menu1);
                menu1.setVisible(true);

            }
        });


        JButton jb2 = new JButton("历史开奖");
        jb2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileReader fr1 = null;
                try {
                    String filePath = System.getProperty("user.dir");  //获得当前程序路径
                    String fileName = filePath+"/History.txt";
                    fr1 = new FileReader(fileName);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                JFrame menu2 = new JFrame("历史开奖");
                JPanel jpanel1 = new JPanel(new FlowLayout());
                JScrollPane scrollPane1 = new JScrollPane();
                scrollPane1.setBounds(80,25, 700, 500);
                jpanel1.add(scrollPane1);
                JTable table1 = new JTable();
                DefaultTableModel dtm1 = new DefaultTableModel(
                        new Object[][] {  },
                        new String[] { "日期","号码", "奖池","特等奖","一等奖" });
                getFile(fr1,dtm1,5);
                table1.setModel(dtm1);
                table1.setBounds(100, 100, 200, 200);
                scrollPane1.setPreferredSize(new Dimension(700, 500));
                scrollPane1.setAutoscrolls(true);
                scrollPane1.setViewportView(table1);
                menu2.add(jpanel1);
                menu2.pack();
                location(menu2);
                menu2.setVisible(true);
            }
        });






        JButton jb3 = new JButton("订单");
        jb3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileReader fr2 = null;
                try {
                    String filePath = System.getProperty("user.dir");  //获得当前程序路径
                    String fileName = filePath+"/Record.txt";
                    fr2 = new FileReader(fileName);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                JFrame menu3 = new JFrame("订单");
                JPanel jpanel2 = new JPanel(new FlowLayout());

                JScrollPane scrollPane1 = new JScrollPane();
                scrollPane1.setBounds(80,25, 700, 500);
                jpanel2.add(scrollPane1);

                JTable table1 = new JTable();

                DefaultTableModel dtm2 = new DefaultTableModel(
                        new Object[][] {  },
                        new String[] { "日期","号码", "倍数","Id" });
                getFile(fr2,dtm2,4);
                table1.setModel(dtm2);
                table1.setBounds(100, 100, 200, 200);

                scrollPane1.setPreferredSize(new Dimension(500, 400));
                scrollPane1.setAutoscrolls(true);
                scrollPane1.setViewportView(table1);
                menu3.add(jpanel2);
                menu3.pack();
                location(menu3);
                menu3.setVisible(true);
            }
        });

        JButton jb4 = new JButton("用户信息");
        jb4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileReader fr3 = null;
                try {
                    String filePath = System.getProperty("user.dir");  //获得当前程序路径
                    String fileName = filePath+"/Users.txt";
                    fr3 = new FileReader(fileName);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                JFrame menu4 = new JFrame("客户信息");
                JPanel jpanel3 = new JPanel(new FlowLayout());

                JScrollPane scrollPane1 = new JScrollPane();
                scrollPane1.setBounds(80,25, 700, 500);
                jpanel3.add(scrollPane1);

                JTable table1 = new JTable();

                DefaultTableModel dtm3 = new DefaultTableModel(
                        new Object[][] {  },
                        new String[] { "ID","用户名", "密码","余额","电话" });
                getFile(fr3,dtm3,6);
                table1.setModel(dtm3);
                table1.setBounds(100, 100, 200, 200);

                scrollPane1.setPreferredSize(new Dimension(500, 400));
                scrollPane1.setAutoscrolls(true);
                scrollPane1.setViewportView(table1);
                menu4.add(jpanel3);
                menu4.pack();
                location(menu4);
                menu4.setVisible(true);
            }
        });

        JButton jb5 = new JButton("测试");
        jb5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JFrame menu = new JFrame("自动注册");
                menu.setLayout(new BorderLayout());
                JPanel jp1 = new JPanel();
                JPanel jp2 = new JPanel();
                JTextField jf = new JTextField(10);
                jf.setFont(new Font("微软雅黑", Font.BOLD, 18));
                JButton jb1 = new JButton("开始");

                jb1.addActionListener((ActionEvent e)->{
                    int num = Integer.parseInt(jf.getText());
                    Thread t1 = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            for(int i = 0;i<num;i++) {
                                new Auto_Register();
                            }
                        }
                    });
                    t1.start();
                    JOptionPane.showMessageDialog(null, "用户注册成功", null, JOptionPane.OK_OPTION);
                });

                jp1.add(jf);
                jp2.add(jb1);
                JLabel jl = new JLabel("请输入注册用户数");
                menu.add(jl, BorderLayout.WEST);
                menu.add(jp1, BorderLayout.CENTER);
                menu.add(jp2, BorderLayout.SOUTH);
                menu.pack();
                location(menu);
                menu.setVisible(true);
            }
        });

        int w = 330;	 int h = 40;
        jb1.setPreferredSize(new Dimension(w, h));
        jb2.setPreferredSize(new Dimension(w, h));
        jb3.setPreferredSize(new Dimension(w, h));
        jb1.setFont(new java.awt.Font("黑体", 1, 20));
        jb2.setFont(new java.awt.Font("黑体", 1, 20));
        jb3.setFont(new java.awt.Font("黑体", 1, 20));
        jb4.setFont(new java.awt.Font("黑体", 1, 20));
        jb5.setFont(new java.awt.Font("黑体", 1, 20));
        panel2.add(jb1);
        panel2.add(jb2);
        panel2.add(jb3);
        panel2.add(jb4);
        panel2.add(jb5);
        panel2.setBorder(new EmptyBorder(20, 20, 20, 20));
        menu.add(panel2);
        menu.pack();
        location(menu);
        menu.setVisible(true);
    }









    public void location(JFrame menu) {
        //int windowWidth = menu.getWidth(); // 获取窗体的宽
        int windowHeight = menu.getHeight();// 获取窗体的高
        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
        Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
        int screenWidth = screenSize.width; // 获取屏幕的宽
        int screenHeight = screenSize.height; // 获取屏幕的高
        menu.setLocation(screenWidth / 2-300, screenHeight / 2 - windowHeight / 2);
    }



    public void getFile(FileReader fr,DefaultTableModel dtm,int n) {
        BufferedReader bf = new BufferedReader(fr);
        String str;
        try {
            while((str=bf.readLine())!=null) {
                String[] strs = str.split(" ");
                if(n==4) dtm.addRow(new String[] {strs[0]+" "+strs[1],strs[2],strs[3],strs[5]});
                else if(n==5) dtm.addRow(new String[] {strs[0]+" "+strs[1],strs[2],strs[3],strs[4],strs[5]});
                else dtm.addRow(new String[] {strs[0],strs[1],strs[2],strs[3],strs[4]});

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }




    @SuppressWarnings({ "deprecation", "unused" })
    public void modifyFile(JTextField jf) {
        BufferedReader record = null;
        PrintWriter record1 = null;
        PrintWriter history = null;
        LinkedList<Tickets> ti = new LinkedList<>();
        int money = 0;
        int special = 0;
        int first = 0;
        try {
            String filePath = System.getProperty("user.dir");  //获得当前程序路径
            String fileName = filePath+"/Record.txt";
            record = new BufferedReader(new FileReader("fileName"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //String str = jf.getText();
        String temp;
        try {
            while((temp = record.readLine())!=null) {
                String[] strs = temp.split(" ");
                Tickets ticket1;
                if(strs[4].equals("NoAward")) strs[4] = GetPrize(strs[2],jf);

                //System.out.println(strs[4]);

                ticket1 = new Tickets(strs[0]+" "+strs[1],strs[5],strs[2].split(","),strs[3],strs[4]);


                money+=Integer.parseInt(strs[3])*2;

                if(strs[4].equals("SpecialAward")) {
                    money-=Integer.parseInt(strs[3])*1000;
                    special++;
                    GetMoney(Integer.parseInt(strs[3])*1000,strs[5]);
                }
                else if(strs[4].equals("FirstAward")) {
                    money-=Integer.parseInt(strs[3])*500;
                    first++;
                    GetMoney(Integer.parseInt(strs[3])*500,strs[5]);
                }

                ti.add(ticket1);

            }
            String filePath1 = System.getProperty("user.dir");  //获得当前程序路径
            String fileName1 = filePath1+"/Record.txt";
            String filePath2 = System.getProperty("user.dir");  //获得当前程序路径
            String fileName2 = filePath2+"/History.txt";
            record1 = new PrintWriter(new BufferedWriter(new FileWriter(fileName1)));
            history = new PrintWriter(new BufferedWriter(new FileWriter(fileName2,true)));

            for(Tickets s:ti) {
                //System.out.println(s.getDate()+" "+s.toString()+" "+s.getTimes()+" "+s.getState()+" "+s.getId());
                record1.println(s.getDate()+" "+s.toString()+" "+s.getTimes()+" "+s.getState()+" "+s.getId());

            }
            Date day = new Date();
            history.println(day.toLocaleString()+" "+jf.getText()+" "+money+" "+special+" "+first);
            record.close();
            record1.close();
            history.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public String GetPrize(String s,JTextField jf) {
        String[] num = s.split(",");
        String[] num1 = jf.getText().split(",");
        int count = 0;
        for(int i = 0;i<num.length;i++) {
            if(num[i].equals(num1[i])) count++;
        }
        if(count==7) return "SpecialAward";
        else if(count==6) return "FirstAward";
        else return "Losing";
    }

    public void GetMoney(int money,String id) {
        BufferedReader record = null;
        PrintWriter record1 = null;
        ArrayList<Users> us = new ArrayList<>();
        try {
            String filePath = System.getProperty("user.dir");  //获得当前程序路径
            String fileName = filePath+"/Users.txt";
            record = new BufferedReader(new FileReader(fileName));

            String str;
            int sum = 0;
            while(( str = record.readLine())!=null) {
                String[] strs = str.split(" ");
                if(id.equals(strs[0])) sum = Integer.parseInt(strs[3])+money;
                Users index = new Users(strs[0],strs[1],strs[2],sum,strs[4]);
                us.add(index);
            }
            record1 = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
            for(Users s:us) {
                //System.out.println(s.getId()+" "+s.getUsername()+" "+s.getPassword()+" "+s.getBalance()+" "+s.getPhone());
                record1.println(s.getId()+" "+s.getUsername()+" "+s.getPassword()+" "+s.getBalance()+" "+s.getPhone());

            }
            record.close();
            record1.close();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void GetMan(JTextField jf,DefaultTableModel dtm) {
        FileReader fr = null;
        String str;
        try {
            String filePath = System.getProperty("user.dir");  //获得当前程序路径
            String fileName = filePath+"/Record.txt";
            fr = new FileReader(fileName);
            BufferedReader bf = new BufferedReader(fr);
            while((str=bf.readLine())!=null) {
                String[] strs = str.split(" ");
                if(jf.getText().equals(strs[2])) dtm.addRow(new String[] {strs[5],strs[3],strs[4]});

            }
            fr.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Manager();
    }


}
